package com.power.sifa.base.service.impl;

import com.plf.core.common.service.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import com.power.sifa.base.model.Mq_logerr;
import com.power.sifa.base.dao.Mq_logerrDao;
import com.power.sifa.base.service.Mq_logerrService;
import com.power.sifa.base.model.pageInfo.Mq_logerrPageInfo;

import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;

/**
 *  队列消费错误记录业务层实现
 */
@Service
public class Mq_logerrServiceImpl extends BaseServiceImpl implements Mq_logerrService {

    @Autowired
    private Mq_logerrDao dao;

    @Override
    public Mq_logerr getById(String id) {
        return dao.findById(id).orElse(null);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdate(Mq_logerr mq_logerr) {
    	// save 如果数据库不存在这条数据，就会执行保存（即将这条数据插入到数据库）；如果数据库存在这条数据，就会根据主键去更新这条数据
        if (mq_logerr.getPrimaryKey() != null) {
        	 Mq_logerr mq_logerrfDb = dao.findById(mq_logerr.getPrimaryKey()).orElse(null);
             if(mq_logerrfDb != null) {
                BeanUtils.copyProperties(mq_logerr, mq_logerrfDb);
                dao.save(mq_logerrfDb);
                return;
             }
        }
        dao.save(mq_logerr);
    }

    @Override
    public void removeById(String id) {
        dao.deleteById(id);
    }

    @Override
    public Page<Mq_logerr> findAll(Mq_logerrPageInfo pageInfo) {
        //排序等定义
        Sort sort;
        if("asc".equals(pageInfo.getOrder())) {
            sort = Sort.by(Sort.Direction.ASC, pageInfo.getSort());
        }else {
            sort = Sort.by(Sort.Direction.DESC, pageInfo.getSort());
        }
        //分页的定义
        Pageable pageable = PageRequest.of(pageInfo.getPageNumber()<=1 ? 0 : pageInfo.getPageNumber()-1, pageInfo.getPageSize(), sort);
        //查询条件
        Specification spec = this.createSpecification(pageInfo);
        return dao.findAll(spec, pageable);
    }

    @Transactional(rollbackFor = Exception.class)
    public void removeByIds(List<String> ids) {
        dao.removeByIds(ids);
    }

    @Override
	public List<Mq_logerr> findAll() {
		return dao.findAll();
	}

	@Override
	public void saveAll(List<Mq_logerr> list) {
		dao.saveAll(list);
	}

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(Mq_logerr mq_logerr) {
        dao.save(mq_logerr);
    }
}
